// DlgFixRequest.cpp : implementation file
//

#include "stdafx.h"
#include "Celsus.h"
#include "DlgFixRequest.h"


// CDlgFixRequest dialog

IMPLEMENT_DYNAMIC(CDlgFixRequest, CDialog)

CDlgFixRequest::CDlgFixRequest(CWnd* pParent /*=NULL*/)
	: CDialog(CDlgFixRequest::IDD, pParent)
	, m_szRequest(_T(""))
	, m_szInformation(_T(""))
{
	m_bCombine = TRUE;
	m_nStaffID = -1;
}

CDlgFixRequest::~CDlgFixRequest()
{
}

void CDlgFixRequest::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	DDX_Text(pDX, IDC_EDIT_REQUEST, m_szRequest);
	DDX_Text(pDX, IDC_STATIC_INFORMATION, m_szInformation);
}


BEGIN_MESSAGE_MAP(CDlgFixRequest, CDialog)
	ON_EN_CHANGE(IDC_EDIT_REQUEST, &CDlgFixRequest::OnEnChangeEditRequest)
END_MESSAGE_MAP()


// CDlgFixRequest message handlers

BOOL CDlgFixRequest::OnInitDialog()
{
	CDialog::OnInitDialog();
	ASSERT( m_nStaffID > -1 );

	// set title
	if( m_bCombine )
	{
		SetWindowText( _T("Combine Requests") );
		m_szInformation.LoadString( IDS_COMBINE_REQUEST_INFO );
	}
	else
	{
		SetWindowText( _T("Split Requests") );
		m_szInformation.LoadString( IDS_SPLIT_REQUEST_INFO );
	}


	// cancel if no requests
	if( m_aRequestID.GetCount() < 1 )
	{
		EndDialog( IDCANCEL );
		return TRUE;
	}

	LONG nUserID = 0;;
	REQUEST_SET Request;

	// get the first for user id
	appConfig.m_SQL.GetRequestSet( &Request, m_aRequestID.GetAt( 0 ) );
	nUserID = Request.m_user_id;

	// cancel if no requests
	if( m_aRequestID.GetCount() < 0 )
	{
		AfxMessageBox( IDS_REQUEST_NO_MATCH );
		EndDialog( IDCANCEL );
		return TRUE;
	}

	// check user and build request	
	for( INT nIndex = 0; nIndex < m_aRequestID.GetCount(); nIndex++ )
	{
		// get request
		if( appConfig.m_SQL.GetRequestSet( &Request, m_aRequestID.GetAt( nIndex ) ))
		{
			// get variables and check
			if( nUserID != Request.m_user_id )
			{
				AfxMessageBox( _T("Different user for this request. Skipping...") );
			}
			else
			{				
				CString szTemp;
				// trim it
				szTemp = Request.m_request.Trim();
				// remove tabs
				szTemp.Remove( '\t' );
				// add it
				m_szRequest += szTemp + _T("\r\n");
			}
		}
	}

	// update data
	UpdateData( FALSE );

	return TRUE;  // return TRUE unless you set the focus to a control
	// EXCEPTION: OCX Property Pages should return FALSE
}

void CDlgFixRequest::OnOK()
{
	// update data
	UpdateData();

	// remove tabs
	m_szRequest.Remove( '\t' );

	REQUEST_SET Request;

	// combine
	if( m_bCombine )
	{
		// get first request
		appConfig.m_SQL.GetRequestSet( &Request, m_aRequestID.GetAt( 0 ) );
		// set variables
		Request.m_request = m_szRequest;
		// update request
		appConfig.m_SQL.SetRequestSet( &Request );

		CString szHistory;
		szHistory = _T("Combined with Requests: ");

		// remove rest
		for( INT nIndex = 1; nIndex < m_aRequestID.GetCount(); nIndex++ )
		{
			// get ID
			CString szTemp;
			szTemp.Format( _T(" %ld"), m_aRequestID.GetAt( nIndex ));
			szHistory += szTemp;
			// remove request
			appConfig.m_SQL.RemoveRequest( m_aRequestID.GetAt( nIndex ) );
		}
		// write history		
		appConfig.UpdateRequestHistory( Request.m_id, m_nStaffID, Request.m_status_id, szHistory );
	}
	else
	{
		// split string
		CStringArray szaRequests;
		appConfig.SplitString( m_szRequest, _T("\n\r\n\r"), szaRequests );

		// should only be one record in array
		ASSERT( m_aRequestID.GetCount() == 1 );
		// get first request
		appConfig.m_SQL.GetRequestSet( &Request, m_aRequestID.GetAt( 0 ) );

		BOOL bExternal = FALSE;
		BOOL bGood = FALSE;
		REQUEST_SET RequestNew;

		// check for external
		if( Request.m_external_id > 0 )
		{
			bExternal = TRUE;
			// inform staff of external
			if( AfxMessageBox( _T("Please note it may be a few minutes for the request to regenerate.\n\nDo you want to continue?"), MB_YESNO ) == IDNO )
				return;
		}

		// loop through requests
		for( INT nIndex = 0; nIndex < szaRequests.GetCount(); nIndex++ )
		{
			// get empty request
			appConfig.m_SQL.GetEmptyRequest( &RequestNew );

			CString szRequest = szaRequests.GetAt( nIndex );
			// trim request
			szRequest.Trim();

			// set request
			RequestNew.m_request = szRequest;
			// set variables
			RequestNew.m_staff_id = Request.m_staff_id;
			RequestNew.m_user_id = Request.m_user_id;
			RequestNew.m_date_in = Request.m_date_in;
			RequestNew.m_date_hold = Request.m_date_hold;
			RequestNew.m_date_out = Request.m_date_out;
			RequestNew.m_status_id = Request.m_status_id;
			RequestNew.m_library_id = Request.m_library_id;
			RequestNew.m_atlibrary = Request.m_atlibrary;
			RequestNew.m_step_id = Request.m_step_id;
			RequestNew.m_instructions = Request.m_instructions;
			RequestNew.m_information = Request.m_information;
			RequestNew.m_notes = Request.m_notes;
			RequestNew.m_uid = _T("");
			RequestNew.m_title = _T("");
			RequestNew.m_priority_id = Request.m_priority_id;
			RequestNew.m_method_id = Request.m_method_id;
			RequestNew.m_ill_id = Request.m_ill_id;
			RequestNew.m_file = _T("");
			RequestNew.m_viewed = 0;
			RequestNew.m_redirected = 0;
			RequestNew.m_touched = 0;
			RequestNew.m_webcheck = 0;
			RequestNew.m_deleted = 0;
			RequestNew.m_invoiced = Request.m_invoiced;
			RequestNew.m_type = Request.m_type;
			RequestNew.m_level = Request.m_level;
			RequestNew.m_taxable = Request.m_taxable;
			RequestNew.m_overdue = Request.m_overdue;
			RequestNew.m_copyright = Request.m_copyright;
			RequestNew.m_canceled = 0;
			RequestNew.m_finalized = 0;
			// add fess
			RequestNew.m_fee_copyright = Request.m_fee_copyright;
			RequestNew.m_fee_service = Request.m_fee_service;
			RequestNew.m_fee_misc = Request.m_fee_misc;
			// added updates
			RequestNew.m_collection_id = Request.m_collection_id;
			RequestNew.m_source = _T("");
			RequestNew.m_project = Request.m_project;
			RequestNew.m_resubmitted = Request.m_resubmitted;
			RequestNew.m_referrals = Request.m_referrals;
			RequestNew.m_copyright = Request.m_copyright;					
			RequestNew.m_manual = Request.m_manual;
			RequestNew.m_material = Request.m_material;
			RequestNew.m_retain = 0;

			// add request
			if( !bExternal )
				bGood = appConfig.m_SQL.SetRequestSet( &RequestNew, -1, TRUE );
			else
				bGood = appConfig.m_SQL.SetExternalRequestSet( &RequestNew, -1, TRUE  );

			// write history
			CString szHistory;
			szHistory.Format( _T("Split form Request: %ld"), Request.m_id );
			appConfig.UpdateRequestHistory( RequestNew.m_id, m_nStaffID, STATUS_ID_NEW, szHistory );
		}
		// remove current
		if( bGood )
			appConfig.m_SQL.RemoveRequest( m_aRequestID.GetAt( 0 ) );
	}

	CDialog::OnOK();
}

void CDlgFixRequest::OnCancel()
{
	// TODO: Add your specialized code here and/or call the base class

	CDialog::OnCancel();
}

void CDlgFixRequest::OnEnChangeEditRequest()
{
	// update data
	UpdateData();

	// split string
	CStringArray szaRequests;
	appConfig.SplitString( m_szRequest, _T("\n\r\n\r"), szaRequests );

	INT nCount = szaRequests.GetCount();
	m_szInformation.Format( _T("Current number of requests: %d"), nCount );

	// update data
	GetDlgItem( IDC_STATIC_INFORMATION )->SetWindowText( m_szInformation );
}
